﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using Modbus通信库demo;
namespace Modbus通信类库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static ModbusRtu ObjModbusRtu = new ModbusRtu();

        private void Btn_Link_Click(object sender, EventArgs e)
        {
            try
            {
                ObjModbusRtu.Connect_Com("COM2", 9600, Parity.None, 8, StopBits.One);

            }
            catch (Exception)
            {

                MessageBox.Show("连接失败");
                return;
            }
            MessageBox.Show("连接成功");
        }

        private void Btn_Broken_Click(object sender, EventArgs e)
        {
            ObjModbusRtu.Close_Port();
        }

        private void Btn_Read_Click(object sender, EventArgs e)
        {
            byte[] ReadingData = ObjModbusRtu.ReadRegister(1, 3, 1);// 从哪开始读取
            this.Text_Result.Text = (ReadingData[0] * 256 + ReadingData[1]).ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            byte[] ReadingData = ObjModbusRtu.ReadRegister(1, 0, 10);// 从哪开始读取
            this.label1.Text = (ReadingData[0] * 256 + ReadingData[1]).ToString();

            // this.label1.Text = "1";
        }
    }
}
